package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import dataLayer.DAOAbsence;
import dataLayer.DBconnect;
import model.Absence;
import model.Teacher;
import types.ConsoleType;
import view.PanelAbsenceList;
import view.PanelAddAbsence;
import view.PanelDASboard;
import view.PanelHolder;

public class AddAbsence extends DBconnect implements ActionListener {
	private int Absence_Id;
	private String Absence_Teacher;
	private String Absence_Substitute;
	public int Absence_Hours;
	public String Absence_Note;
	public Date Absence_StartDate;
	public Date Absence_EndDate;
	public String Absence_Reason;
	private PanelAddAbsence PAAform;
	private String parse;

	public AddAbsence(PanelAddAbsence PAAform) {
		this.PAAform = PAAform;

	}

	// Check on fields for addAbsence and Error message in console
	public void actionPerformed(ActionEvent e) {
		openConn();
		if (e.getActionCommand().equals("Opslaan")) {
			String message = null;
			ConsoleType type = null;
			try {

				Absence_Note = PAAform.getJTNote().getText();
				Absence_StartDate = (Date) PAAform.getJSStartDate().getValue();
				Absence_EndDate = (Date) PAAform.getJSEndDate().getValue();
				Absence_Substitute = (String) PAAform.getInvallerCombo()
						.getSelectedItem();

				
				Absence_Teacher = (String) PAAform.getUitvallerCombo()
						.getSelectedItem();

				Absence_Reason = (String) PAAform.getRedenCombo()
						.getSelectedItem();
			
				// Absence_Hours = (int) Integer.parseInt(PAAform.getJTHours()
				// .getText());
				// TODO Absence Hours is 1, moet uitgerekend worden adhv het
				// rooster!
				Absence_Hours = 1;
				if (Absence_Teacher.equals(Absence_Substitute)) {
					message = "Een docent kan niet voor zichtzelf invallen";
					type = ConsoleType.ERROR;

				} else {
					//TODO de if werkt niet als je de einddatum eerst veranderd en dan terug zetten op de huidige dag, maar moet wel werken.
					if (Absence_StartDate.after(Absence_EndDate)
							&& !(Absence_StartDate.equals(Absence_EndDate))) {
						message = "De begindatum kan niet na de einddatum liggen";
						type = ConsoleType.ERROR;
					} else {
						//adds the absence to the database
						Absence a = new Absence(Absence_Id, Absence_Teacher,
								Absence_Substitute, Absence_Hours,
								Absence_Note, Absence_StartDate,
								Absence_EndDate, Absence_Reason);
						
						
						Date ED = new Date(a.Absence_EndDate.getTime());
						System.out.println(ED + "  add absencce");
						
						
						Absence a1 = new Absence(Absence_Id, Absence_Teacher,
								Absence_Substitute, Absence_Hours,
								Absence_Note, Absence_StartDate,
								ED, Absence_Reason);
						
						
						Teacher.getInstance().addAbsence(a1);
						DAOAbsence dbG = DAOAbsence.getInstance();
						
						dbG.InsertAbsence(a);
						Teacher.getInstance().getAllAbsencesToday().add(a1);
						Teacher.getInstance().getAllAbsencesWeek().add(a1);
						
						
						PanelHolder ph = PanelHolder.getInstance();
						ph.remove(PAAform);
						if(PanelAddAbsence.getInstance().previousPage == 1){
							ph.add(PanelDASboard.getInstance());
						}
						if(PanelAddAbsence.getInstance().previousPage == 2){
							ph.add(PanelAbsenceList.getInstance());
						}
						ph.repaint();
						ph.revalidate();
						

						message = " De absentie is opgeslagen";
						type = ConsoleType.SUCCESS;
					}

				}
				Console.getInstance().setConsole(message, type);

			} catch (Exception e2) {

			}
		}
	}

}
